<?php

function parse_platform( $batch, $platform ) {
    $context = getContext('GET');
    echo $context;

    $url = 'http://www.crowdcube.com/investments?hof=1';
    $html = file_get_html($url, false, $context);
    
    $a1 = $html->find('article[class="clearfix pitch animate"]');
    
    $url = 'http://www.crowdcube.com/investments';
    $html = file_get_html($url, false, $context);
    
    $a2 = $html->find('article[class="clearfix pitch animate"]');
    
    $articles = array_merge($a1,$a2);

    foreach($articles as $article) {
        
        $h2 = $article->find('h2',0);
        $image = "http://www.crowdcube.com" . $article->find('img',0)->src;
        $link = $h2->find('a',0)->href;
        $name = $h2->find('a',0)->plaintext;
        $description = str_replace('Read full pitch','',$article->find('p',0)->plaintext);
        $external_id = substr($link,strrpos($link,'-')+1);
        $currency= '£';
        
        $ul = $article->find('ul[class=pitch-stats clearfix]',0);
        foreach ($ul->find('li') as $li){ 
            $type = $li->find('small',0)->plaintext;
            
            switch ($type) {
                case 'days left':
                $days = trim(str_replace($type,'',$li->plaintext));
                break;
                case 'invested':
                $current = trim(str_replace(',','',str_replace($currency,'',str_replace($type,'',$li->plaintext))));
                break;
                case 'target':
                $target = trim(str_replace(',','',str_replace($currency,'',str_replace($type,'',$li->plaintext))));
                break;
                case 'equity':
                $shares = trim(str_replace('%','',str_replace($type,'',$li->plaintext)));
                break;
            }
        }
        $worth = '0';
        $active = $days > 0 ? 1 : 0;
        
        if($batch) {
            saveProjectData($external_id, $platform, $batch, $current, $days, $worth, $shares, $target, $image, $link, $name, $description, $active, $currency);
        }
    }
}
?>